<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>
    PHP Decorator 装饰器模式 |
    
    无敌小笼包</title>
  
    <link rel="shortcut icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  
    <link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">
  
  <script src="/js/pace.min.js"></script>
</head>

<body>
<main class="content">
  <section class="outer">
  <article id="post-Decorator" class="article article-type-post" itemscope itemprop="blogPost">

  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      PHP Decorator 装饰器模式
    </h1>
  

      </header>
    

    
      <div class="article-meta">
        <a href="/2017/02/23/Decorator/" class="article-date">
  <time datetime="2017-02-23T02:20:12.000Z" itemprop="datePublished">2017-02-23</time>
</a>
        
  <div class="article-category">
    <a class="article-category-link" href="/categories/Skill/">Skill</a>
  </div>

      </div>
    

    <div class="article-entry" itemprop="articleBody">
      
      
      
        <ul>
<li>如果已有对象的部分内容或功能性发生改变，但是不需要修改原始的结构</li>
</ul>
<p>迭代新的应用需求</p>
<a id="more"></a>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * Decorator 装饰器模式</div><div class="line"> * -------------------------------------</div><div class="line"> * ** 来自说明 **</div><div class="line"> *</div><div class="line"> * 如果已有对象的部分内容或功能性发生改变，但是不需要修改原始的结构</div><div class="line"> *</div><div class="line"> * 刚开始楼主觉得这个他么怎么这么像 【 适配器模式 】</div><div class="line"> * 然后又把适配器模式的理论复制了过来给各位观众老爷看</div><div class="line"> * </div><div class="line"> * |-&gt; 适配器模式： * 通过适配器模式能够使用新的代码和功能性来帮助更新原有的系统。</div><div class="line"> *</div><div class="line"> * 前面演练过了，适配器还是需要继承原对象来实现，但是我们最开始学习面向对象编程时候</div><div class="line"> * 如果对象开始要求启用过多的子类，那么相应的代码就会牺牲编程人员的理解力和可维护性</div><div class="line"> * 通常，我们会竭力保证用于一个对象的父-子类关系不超过3个</div><div class="line"> * </div><div class="line"> * </div><div class="line"> * ===================================== </div><div class="line"> * ** 应用场景 **</div><div class="line"> *</div><div class="line"> * 迭代新的应用需求</div><div class="line"> * </div><div class="line"> * -------------------------------------</div><div class="line"> * </div><div class="line"> * <span class="doctag">@version</span> $&#123;Id&#125;$</div><div class="line"> * <span class="doctag">@author</span> Shaowei Pu &lt;54268491<span class="doctag">@qq</span>.com&gt;</div><div class="line"> */</div></pre></td></tr></table></figure>
<hr>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="comment">// 现在你是一个农场饲养员，主要负责养猪方面工作</span></div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 饲养类</div><div class="line"> */</div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">feeding</span></span>&#123;</div><div class="line"></div><div class="line">	<span class="comment">/**</span></div><div class="line">	 * [$mess_tin 饭盒]</div><div class="line">	 * <span class="doctag">@var</span> array</div><div class="line">	 */</div><div class="line">	<span class="keyword">public</span> $mess_tin = [];</div><div class="line"></div><div class="line">	<span class="comment">/**</span></div><div class="line">	 * [feed 喂食物]</div><div class="line">	 * <span class="doctag">@author</span> 		Shaowei Pu &lt;pushaowei<span class="doctag">@sporte</span>.cn&gt;</div><div class="line">	 * <span class="doctag">@CreateTime</span>	2017-02-23T19:47:18+0800</div><div class="line">	 * <span class="doctag">@param</span>                               [type] $food [description]</div><div class="line">	 * <span class="doctag">@return</span>                              [type]       [description]</div><div class="line">	 */</div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">feed</span><span class="params">( $food )</span></span>&#123;</div><div class="line">		<span class="keyword">$this</span>-&gt;mess_tin[] = $food;</div><div class="line">	&#125;</div><div class="line">	<span class="comment">/**</span></div><div class="line">	 * [getHow 吃了哪些东西]</div><div class="line">	 * <span class="doctag">@author</span> 		Shaowei Pu &lt;pushaowei<span class="doctag">@sporte</span>.cn&gt;</div><div class="line">	 * <span class="doctag">@CreateTime</span>	2017-02-23T19:51:41+0800</div><div class="line">	 * <span class="doctag">@return</span>                              [type] [description]</div><div class="line">	 */</div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getHow</span><span class="params">()</span></span>&#123;</div><div class="line">		<span class="keyword">return</span> <span class="keyword">$this</span>-&gt;mess_tin;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 以前你通过这样的方式可以喂猪</span></div><div class="line">$you = <span class="keyword">new</span> feeding;</div><div class="line"></div><div class="line"><span class="comment">// 然后老板给了这些食物</span></div><div class="line">$bread = [		</div><div class="line">		<span class="string">'apple'</span>,</div><div class="line">		<span class="string">'tangerine'</span>,</div><div class="line">		<span class="string">'banana'</span>,</div><div class="line">	];</div><div class="line"></div><div class="line"><span class="keyword">foreach</span> ($bread <span class="keyword">as</span> $key =&gt; $value) &#123;</div><div class="line">	$you-&gt;feed($value);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 看看吃了些啥</span></div><div class="line">var_dump( $you-&gt;getHow() );</div><div class="line"><span class="comment">/* </span></div><div class="line">+----------------------------------------------------------------------</div><div class="line">|array (size=3)</div><div class="line">|  0 =&gt; string 'apple' (length=5)</div><div class="line">|  1 =&gt; string 'tangerine' (length=9)</div><div class="line">|  2 =&gt; string 'banana' (length=6)</div><div class="line">+----------------------------------------------------------------------</div><div class="line">*/</div><div class="line"></div><div class="line"><span class="comment">// 但是有天高级饲养说这喂的这些食物都是小写的 这不行，给猪必须要大写的</span></div><div class="line"><span class="comment">// 然后你看了下饲养类，听隔壁兄弟这个类不仅仅用在猪身上</span></div><div class="line"><span class="comment">// 也有养羊事业部，羊牛事业部，同样用的这个类</span></div><div class="line"><span class="comment">// 你不能瞎几把乱改</span></div><div class="line"><span class="comment">// 学了适配器的你马上想到了 使用【适配器模式】可以解决问题</span></div><div class="line"><span class="comment">// 但是立马老大就说了不准乱继承。一个父类最多继承 3 个子类</span></div><div class="line"><span class="comment">// 于是聪明的你想到了这样一个方法</span></div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 养猪事业部专用</div><div class="line"> */</div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">feddingDecorator</span></span>&#123;</div><div class="line"></div><div class="line">	<span class="comment">/**</span></div><div class="line">	 * [$_feeding 基类容器]</div><div class="line">	 * <span class="doctag">@var</span> array</div><div class="line">	 */</div><div class="line">	<span class="keyword">private</span> $_feeding = [];</div><div class="line"></div><div class="line">	<span class="comment">/**</span></div><div class="line">	 * [__construct 基类入变量]</div><div class="line">	 * <span class="doctag">@author</span> 		Shaowei Pu &lt;pushaowei<span class="doctag">@sporte</span>.cn&gt;</div><div class="line">	 * <span class="doctag">@CreateTime</span>	2017-02-23T20:00:58+0800</div><div class="line">	 * <span class="doctag">@param</span>                               feeding $feeding [description]</div><div class="line">	 */</div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">( feeding $feeding )</span></span></div><div class="line">	&#123;</div><div class="line">		<span class="keyword">$this</span>-&gt;_feeding = $feeding;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	<span class="comment">/**</span></div><div class="line">	 * [expertFeed 高级食物转换器]</div><div class="line">	 * <span class="doctag">@author</span> 		Shaowei Pu &lt;pushaowei<span class="doctag">@sporte</span>.cn&gt;</div><div class="line">	 * <span class="doctag">@CreateTime</span>	2017-02-23T20:04:42+0800</div><div class="line">	 * <span class="doctag">@return</span>                              [type] [description]</div><div class="line">	 */</div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">expertFeed</span><span class="params">()</span></span></div><div class="line">	&#123;</div><div class="line">		array_walk(<span class="keyword">$this</span>-&gt;_feeding-&gt;mess_tin, <span class="function"><span class="keyword">function</span><span class="params">( &amp;$value )</span></span>&#123;</div><div class="line">			$value = strtoupper( $value );</div><div class="line">		&#125;);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 然后这样,前期您还是放心喂</span></div><div class="line"></div><div class="line">$me = <span class="keyword">new</span> feeding;</div><div class="line"><span class="keyword">foreach</span> ($bread <span class="keyword">as</span> $key =&gt; $value) &#123;</div><div class="line">	$me-&gt;feed($value);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 然后你喂完了就开始用的养猪专业部使用的高科技了</span></div><div class="line">$stm = <span class="keyword">new</span> feddingDecorator( $me );</div><div class="line">$stm-&gt;expertFeed();</div><div class="line"><span class="comment">// 看看吃了啥</span></div><div class="line">var_dump($me-&gt; getHow());</div><div class="line"></div><div class="line"><span class="comment">/* </span></div><div class="line">+----------------------------------------------------------------------</div><div class="line">|array (size=3)</div><div class="line">| 0 =&gt; string 'APPLE' (length=5)</div><div class="line">| 1 =&gt; string 'TANGERINE' (length=9)</div><div class="line">| 2 =&gt; string 'BANANA' (length=6)</div><div class="line">+----------------------------------------------------------------------</div><div class="line">*/</div><div class="line"></div><div class="line"><span class="comment">// 掌声经久不息</span></div></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://blog.mango16.cc/2017/02/23/Decorator/" data-id="cjrbtw1xn000a6d8hfy48ubo2"
         class="article-share-link">分享</a>
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Design-Pattern/">Design Pattern</a></li></ul>

    </footer>

  </div>

  
    
  <nav class="article-nav">
    
      <a href="/2017/03/10/everyday/" class="article-nav-link">
        <strong class="article-nav-caption">前一篇</strong>
        <div class="article-nav-title">
          
            But you should have one thing mind.
          
        </div>
      </a>
    
    
      <a href="/2017/02/22/Builder/" class="article-nav-link">
        <strong class="article-nav-caption">后一篇</strong>
        <div class="article-nav-title">PHP Builder 建造者模式</div>
      </a>
    
  </nav>


  

  
    
  <div class="gitalk" id="gitalk-container"></div>
  <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
  <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
  <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
  <script type="text/javascript">
      var gitalk = new Gitalk({
        clientID: 'caba8826a61790936307',
        clientSecret: '09a15037dcab30d9adf35a53ec6961939169f797',
        repo: 'm9rco.github.io',
        owner: 'm9rco',
        admin: ['m9rco'],
        // id: location.pathname,      // Ensure uniqueness and length less than 50
        id: md5(location.pathname),
        distractionFreeMode: false  // Facebook-like distraction free mode
      })

  gitalk.render('gitalk-container')
  </script>

  

</article>



</section>
  <footer class="footer">
  
  <div class="outer">
    <ul class="list-inline">
      <li>&copy; 2019 无敌小笼包</li>
      <i class="pulse"></i>
    </ul>
  </div>
</footer>

</main>
<aside class="sidebar">
  <button class="navbar-toggle"></button>

<nav class="navbar">
  
    <div class="logo">
      <a href="/"><img src="/images/hexo.svg" alt="无敌小笼包"></a>
    </div>
  
  <ul class="nav nav-main">
    
      <li class="nav-item">
        <a class="nav-item-link" href="/">主页</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/archives">归档</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/gallery">相册</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/nagging">叽咕</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/about">关于</a>
      </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="Search">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>

<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
        <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
          <i class="fe fe-feed"></i>
        </a>
      
    </li>
  </ul>
</nav>

<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
</aside>
  <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>


  <script src="/fancybox/jquery.fancybox.min.js"></script>



  <script src="/js/search.js"></script>


<script src="/js/ocean.js"></script>

</body>
</html>